package com.zoshine.zzg.controller;

import com.zoshine.zzg.constant.Session;
import com.zoshine.zzg.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 控制层基础
 * @author 贾俊峰
 * @time 2017/7/31 14:37
 */
@Controller
public abstract class BaseController {
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    @ModelAttribute
    public void setSession(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        this.request = request;
        this.response = response;
        this.session = session;
    }

    /**
     * 获取当前登录的用户
     */
    protected User getLoginUser() {
        return (User) session.getAttribute(Session.LOGIN_USER);
    }

    /**
     * 获取当前登录用户的id
     */
    protected Long getLoginUserId() {
        return getLoginUser().getId();
    }

    /**
     * 获取当前登录用户真实姓名
     */
    protected String getLoginUserName() {
        return getLoginUser().getRealName();
    }

    /**
     * 获取用户的供应商权限
     */
    @SuppressWarnings("unchecked")
    protected List<Long> getSupplierPower() {
        return (List<Long>) session.getAttribute(Session.SUPPLIER_POWER);
    }

    /**
     * 获取用户的监所权限
     */
    @SuppressWarnings("unchecked")
    protected List<Long> getPrisonPower() {
        return (List<Long>) session.getAttribute(Session.PRISON_POWER);
    }
}
